package com.gkj.blog.controller;


import com.gkj.blog.business.consts.HttpConsts;
import com.gkj.blog.business.dto.ImgUploadDto;
import com.gkj.blog.business.exception.FileException;
import com.gkj.blog.util.DateUtil;
import com.gkj.blog.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api")
public class ImageController {
    private static Logger LOGGER= LoggerFactory.getLogger(ImageController.class);
    private static String IMAGE_SAVE_ROOT_PATH="E://upload/";

    @GetMapping("/img/{date}")
    public void getImg(HttpServletResponse response, @PathVariable(value = "date") String date, String id){
        String localpath = date + File.separator + id;
        try {
            response.setContentType("image/png");
            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
            File localFile = new File(IMAGE_SAVE_ROOT_PATH + localpath);
            BufferedInputStream in;
            if (localFile.exists()) {
                byte[] buffer = new byte[2048];
                int len = 0;
                in = new BufferedInputStream(new FileInputStream(localFile));
                while ((len = in.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
            } else {
                LOGGER.error("找不到文件{}",localpath);
            }
        }catch (Exception e){
            LOGGER.error("读取文件出错{}",localpath);
        }
    }
}


